﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;

    public class AccessorialCharges : BasePage
    {
        protected Button btnAccessorialCharges;
        protected Button btnAccessorialChargesOrders;
        protected Button btnAccessorialDelete;
        protected Button btnGoBack;
        protected HtmlForm F9181;
        protected GridView gvAccessorialChatgesDetail;
        protected GridView gvReceiptDetail;
        protected HtmlInputHidden hdnAccessorialChargesID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected Label lblAccessorialType;
        protected Label lblMsg;
        protected Label lblOrderID;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblStatus;
        protected Label lblType;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAccessorialType;
        protected TextBox txtOrderID;
        protected TextBox txtOwnerID;
        protected TextBox txtStatus;
        protected TextBox txtType;
        protected UpdatePanel up1;

        protected void btnAccessorialCharges_Click(object sender, EventArgs e)
        {
            List<string> selectedOrderID = base.GetSelectedIDsInGridView(this.gvReceiptDetail, "OrderID");
            List<string> selectedOrderLineID = base.GetSelectedIDsInGridView(this.gvReceiptDetail, "OrderLineID");
            List<string> selectedSkuID = base.GetSelectedIDsInGridView(this.gvReceiptDetail, "SkuID");
            List<string> selectedLot = base.GetSelectedIDsInGridView(this.gvReceiptDetail, "Lot");
            List<string> selectedQty = base.GetSelectedIDsInGridView(this.gvReceiptDetail, "Qty");
            try
            {
                if ((selectedOrderID == null) || (selectedOrderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9180_001");
                }
                else if (selectedOrderID.Count > 1)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9180_002");
                }
                else
                {
                    base.Response.Redirect(string.Format("AccessorialChargesDetail.aspx?from=list&OrderID={0}&LineID={1}&SkuID={2}&Lot={3}&OwnerID={4}&Type={5}&Qty={6}", new object[] { base.UrlEncode(selectedOrderID[0]), base.UrlEncode(selectedOrderLineID[0]), base.UrlEncode(selectedSkuID[0]), base.UrlEncode(selectedLot[0]), base.UrlEncode(this.txtOwnerID.Text), base.UrlEncode(this.hdnType.Value), base.UrlEncode(selectedQty[0]) }));
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
            }
        }

        protected void btnAccessorialChargesOrders_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("AccessorialChargesDetail.aspx?from=list&OrderID={0}&Type={1}&OwnerID={2}&Key=Order", base.UrlEncode(this.txtOrderID.Text), base.UrlEncode(this.hdnType.Value), base.UrlEncode(this.txtOwnerID.Text)));
        }

        protected void btnAccessorialDelete_Clikc(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvAccessorialChatgesDetail, "AccessorialChargesID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9180_001");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9180_002");
            }
            else
            {
                try
                {
                    this.lblMsg.Text = BillingServiceClientProxy.GetInstance().DeleteAccessorialCharges(selectedID) + base.GetGlobalMsgResource("F9180_003");
                    this.SetBillingServiceInfo2Form();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,3) </script>", false);
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("AccessorialChargesQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("AccessorialChargesList.aspx?AccessorialType=" + base.UrlEncode(this.hdnType.Value), false);
                    break;
            }
        }

        public string ConvertLine(string lineID)
        {
            string strValue = lineID;
            if (strValue == "0")
            {
                strValue = base.GetGlobalMsgResource("F9180_004");
            }
            return strValue;
        }

        protected void gvAccessorialChatgesDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9181", this.gvAccessorialChatgesDetail);
        }

        protected void gvAccessorialChatgesDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as AccessorialChargesOrderInfo).AccessorialChargesInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,3) </script>", false);
        }

        protected void gvAccessorialChatgesDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvAccessorialChatgesDetail.PageIndex * this.gvAccessorialChatgesDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvReceiptDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9181", this.gvReceiptDetail);
        }

        protected void gvReceiptDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as AccessorialChargesOrderInfo).AccessorialChargesOrderDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
        }

        protected void gvReceiptDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvReceiptDetail.PageIndex * this.gvReceiptDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnAccessorialChargesID.Value = base.Request.QueryString["ID"];
                this.hdnType.Value = base.Request.QueryString["Type"];
                this.SetBillingServiceInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,3) </script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetBillingServiceInfo2Form()
        {
            try
            {
                AccessorialChargesOrderInfo accessorialChargesOrderInfo = BillingServiceClientProxy.GetInstance().GetAccessorialChargesByID(this.hdnAccessorialChargesID.Value, this.hdnType.Value);
                if (accessorialChargesOrderInfo != null)
                {
                    this.txtOrderID.Text = accessorialChargesOrderInfo.OrderID;
                    this.txtOwnerID.Text = accessorialChargesOrderInfo.OwnerID;
                    this.txtType.Text = accessorialChargesOrderInfo.Type;
                    this.txtAccessorialType.Text = this.hdnType.Value;
                    this.txtStatus.Text = accessorialChargesOrderInfo.Status;
                    base.BindData2GridView(this.gvReceiptDetail, accessorialChargesOrderInfo.AccessorialChargesOrderDetailInfos);
                    base.BindData2GridView(this.gvAccessorialChatgesDetail, accessorialChargesOrderInfo.AccessorialChargesInfos);
                    base.DataEntityViewState = accessorialChargesOrderInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

